package com.neusoft.databus.core.repository.impl;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.model.MonitorData;
import com.neusoft.databus.common.model.QMonitorData;
import com.neusoft.databus.core.repository.CustomizedMonitorDataRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * @author naxiang
 */
public class CustomizedMonitorDataRepositoryImpl extends AbstractRepository implements CustomizedMonitorDataRepository {

    @Override
    public Page<MonitorData> search(Pageable pageable, Searchable searchable) {
        if (null == searchable.getLongValue("endTime")) {
            searchable.put("endTime", new Date().getTime());
        }
        final QMonitorData qMonitorData = QMonitorData.monitorData;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(super.afterOrEqualsDate(qMonitorData.collectedTime, searchable, "startTime"));
        where.and(super.beforeOrEqualsDate(qMonitorData.collectedTime, searchable, "endTime"));
        where.and(super.inStrings(qMonitorData.resource.code, searchable, "resourceCode"));
        where.and(super.containsStr(qMonitorData.resource.name, searchable, "resourceName"));
        final JPAQuery<MonitorData> query = this.query().selectFrom(qMonitorData).where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return MonitorData.class;
    }

}
